#include <cstdio>
#include <cstddef>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

#include <dirent.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>

using namespace std;

#include "main.hpp"
#include "string-lib.hpp"

bool running;
bool verbose;
std::string programName;
std::vector <ServerInfo> serverList;
std::string basedir;
std::string userdir;

bool operator< (ServerInfo i, ServerInfo j)
{
    return i.name < j.name;
}

void loadServer(string file)
{
    ifstream serverFile(file.c_str(), fstream::in);
    vector <string> tokens;
    while (!serverFile.eof())
    {
        ServerInfo server;
        string line;
        getline(serverFile, line);
        stringlib::tokenise(line, tokens);
        if (tokens.size() == 3)
        {
            server.name = tokens[0];
            server.host = tokens[1];
            server.port = tokens[2];
            serverList.push_back(server);
        }
        continue;
    }
    serverFile.close();
}

void printHelp()
{
    cout << "Usage: " << programName << " [options]" << endl;
    cout << "    -h --help              Display this usage information." << endl;
    cout << "    -b --base  directory   Use the specified directory to load the resources." << endl;
}

int main (int argc, char* argv[])
{
    programName = argv[0];
    verbose = false;
    running = true;
    basedir = "resources/";
    //basedir = "/usr/share/checkmate";
    userdir = "~/.checkmate/";

    loadServer(basedir + "servers");
    //loadServer(userdir + "servers");
    sort(serverList.begin(), serverList.end());

    runGUI();
}
